# -*-coding:Utf-8 -*
""" Childs systeme
"""

class ChildList(object):

	def __init__(self, parent, childType = None):
		self._list = []
		self._parent = parent
		self._cildtype = parent.__class__
		object.__init__(self)

	def __len__(self):
		return len(self._list)
	def __contains__(self, child):
		try:
			return self._list.index(child)
		except ValueError:
			return False

	def __getitem__(self, key):
		key = int(key)
		if key < len(self._list):
			return self._list[key]
		else:
			raise IndexError("Child index out of range")
	def __delitem__(self, key):
		key = int(key)
		if key < len(self._list):
			self._list.pop(key)
		else:
			raise IndexError("Child index out of range")
	def __iter__(self):
		safe_list = list(self._list)
		for anElement in safe_list:
			yield anElement

	def add(self, *childs, warning=True):
		for aChild in childs:
			if isinstance(aChild, self._cildtype) and aChild != self._parent:
				self._list.append(aChild)
				aChild._parent = self._parent
	
	def executeForAll(self, func, *params):
		if type(func) == str:
			for aChild in self._list:
				getattr(aChild, func)(*params)
		elif callable(func):
			for aChild in self._list:
				func(aChild, *params)
